package com.naizhuai.ui.mianbusiness.action.presenter;

import com.naizhuai.AppContext;
import com.naizhuai.network.Config;
import com.naizhuai.network.RetrofitServerFactory;
import com.naizhuai.network.apistore.DownloadImageRequest;
import com.naizhuai.network.apistore.UploadImageRequest;
import com.naizhuai.network.upload.FileUpload;
import com.naizhuai.ui.entity.ImageBean;
import com.naizhuai.util.FileUtils;
import com.naizhuai.util.RxUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 处理公共搜索的类
 * Created by code_han on 2018/1/25.
 */

public class UploadImageApiStoreIml implements UploadImageApiStore {

    public static final String MODULE_NAME = "adp";

    private static UploadImageApiStore INSTANCE = null;

    public static UploadImageApiStore getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new UploadImageApiStoreIml();
        }
        return INSTANCE;
    }


    @Override
    public Observable<List<ImageBean>> uploadImagesByUrl(List<String> filePath) {
        Map<String, String> map = new HashMap<>();
        List<MultipartBody.Part> parts=new ArrayList<>();

        map.put("module", MODULE_NAME);

        for (String path : filePath) {
            MultipartBody.Part part1=FileUpload.getParts(path);
            parts.add(part1);
        }
        return createBaseClass().uploadImgs(map, parts).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<ImageBean> uploadAftImageByUrl(String filePath) {
        Map<String, String> map = new HashMap<>();
        map.put("module", MODULE_NAME);
        MultipartBody.Part part=FileUpload.getPart(filePath);
        return createBaseClass().uploadImg(map, part).compose(RxUtils.applySchedulers());
    }


    private UploadImageRequest createBaseClass() {
        return RetrofitServerFactory.createDownload(AppContext.getInstance(), UploadImageRequest.class, Config.COMPANY_COMMON_BASE_URL);
    }

}
